package com.doubi.common.config.dfa;

import com.doubi.utils.SensitiveWordUtil;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

public class DFAInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest servletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        // 如果不是映射到方法直接通过
        if (!(object instanceof HandlerMethod)) {
            return true;
        }
        //获取前端传递的所有参数名的枚举
        Enumeration<String> pNames = servletRequest.getParameterNames();
        //遍历枚举
        while (pNames.hasMoreElements()) {
            if(SensitiveWordUtil.contains(servletRequest.getParameter(pNames.nextElement()))){
                throw new DFAException();
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object o, Exception e) throws Exception {
    }
}
